<?php

namespace App\Http\Controllers\Wechat;

use App\Models\Wechat\Draw;
use App\Models\Wechat\Prize;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class DrawController extends Controller
{
    public function index()
    {
        $draws = Prize::with('customers')->get();
        return view('wechat.draw.index', compact('draws'));
    }

    public function store()
    {
        $rand = rand(100000, 999999);
        if ($rand < 900000) {
            return response()->json(['success' => false, 'data' => ['id' => 8, 'name' => '谢谢抽奖']]);
        }
        $draw = Draw::all();
        $result = countWeight($draw);

        //当中将时存入数据库
        if ($result != '谢谢参与') {
            //模拟三个用户抽奖
            $test = [4, 5, 6];
            $customer_id = $test[rand(0, 2)];

            Prize::create([
                'customer_id' => $customer_id,
                'draw_id' => $result->id
            ]);
        }

        return response()->json(['success' => true, 'data' => $result]);
    }
}
